'use strict';

angular.
module('addQuestion').
component('addQuestion', {
    templateUrl: 'components/addQuestion/addQuestion.template.html',
    controller: function AddQuestionController($scope, $http, $stateParams, $timeout, $injector) {

        /*题目*/
        $scope.question = {};

        $scope.multi_select_answer = {};

        /*题目类型*/
        $scope.type = '单选题';

        $scope.submit = function() {
            if($scope.type == '单选题') {
                $scope.question.select.type = '单选题';
                //保存选择题
                $http.post('/questions/add', $scope.question.select).then(function (response) {
                    //保存成功
                    $(".save-success").removeClass("in").show();
                    $(".save-success").delay(200).addClass("in").fadeOut(2000);
                }, function (error) {
                    //保存失败
                    $(".save-fail").removeClass("in").show();
                    $(".save-fail").delay(200).addClass("in").fadeOut(2000);
                });
            }else if($scope.type == '多选题') {
                $scope.question.multi_select.type = '多选题';
                var answer = '';
                if($scope.multi_select_answer.optionA) {
                    answer = answer + 'A,'
                }
                if($scope.multi_select_answer.optionB) {
                    answer = answer + 'B,'
                }
                if($scope.multi_select_answer.optionC) {
                    answer = answer + 'C,'
                }
                if($scope.multi_select_answer.optionD) {
                    answer = answer + 'D,'
                }
                if(answer.length>1) {
                    answer.substr(0, answer.length - 1);
                }
                $scope.question.multi_select.answer = answer;
                //保存选择题
                $http.post('/questions/add', $scope.question.multi_select).then(function (response) {
                    //保存成功
                    $(".save-success").removeClass("in").show();
                    $(".save-success").delay(200).addClass("in").fadeOut(2000);
                }, function (error) {
                    //保存失败
                    $(".save-fail").removeClass("in").show();
                    $(".save-fail").delay(200).addClass("in").fadeOut(2000);
                });
            }else if($scope.type == '填空题') {
                $scope.question.simple.type = '填空题';
                //保存简答题
                $http.post('/questions/add', $scope.question.simple).then(function (response) {
                    //保存成功
                    $(".save-success").removeClass("in").show();
                    $(".save-success").delay(200).addClass("in").fadeOut(2000);
                }, function (error) {
                    //保存失败
                    $(".save-fail").removeClass("in").show();
                    $(".save-fail").delay(200).addClass("in").fadeOut(2000);
                });
            }else if($scope.type == '判断题') {
                $scope.question.judgment.type = '判断题';
                //保存简答题
                $http.post('/questions/add', $scope.question.judgment).then(function (response) {
                    //保存成功
                    $(".save-success").removeClass("in").show();
                    $(".save-success").delay(200).addClass("in").fadeOut(2000);
                }, function (error) {
                    //保存失败
                    $(".save-fail").removeClass("in").show();
                    $(".save-fail").delay(200).addClass("in").fadeOut(2000);
                });
            }
        };

    }
});